<?php
!defined('IN_FRAME') && die('404 Page');
class c_manager extends Controller {
    
    private $arrCurManager;
    private $mdlManager;
    private $mdlAcl;
    private $arrGroup;
    
    /**
     * 构造函数
     */
    public function __construct() {
        $this->mdlAcl = Helper::loadModel('acl');
        $this->arrGroup = $this->mdlAcl->getGroups();
        $this->mdlManager = Helper::loadModel('manager');
        if (!$this->mdlManager->logined()) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '请先登录!');
        } else {
            $this->arrCurManager = $this->mdlManager->getSession();
        }
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'manager', 'view')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权查看!');
        }
    }
    
    public function index() {
        $arrAssign = array(
            'blnAdd' => true,
            'strTitle' => '管理员列表',
            'arrGroup' => $this->arrGroup,
            'arrList' => $this->mdlManager->select()
        );
        $this->_display('admin/manager_list.php', $arrAssign);
    }
    
    public function add() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'manager', 'add')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=manager', '你无权添加!');
        }
        $arrAssign = array(
            'blnAdd' => true,
            'strTitle' => '添加管理员',
            'arrData' => array(),
            'arrGroup' => $this->arrGroup,
            'strMsg' => '',
            'strOk' => ''
        );
        $strMsg = '';
        if (isset($_POST['sub_btn'])) {
            unset($_POST['sub_btn']);
            $bln = true;
            if ($bln && (!isset($_POST['group_name']) || !isset($this->arrGroup[$_POST['group_name']]))) {
                $bln = false;
                $strMsg = '请选择用户组!';
            }
            if ($bln && (!isset($_POST['user_name']) || !$this->mdlManager->isUserName($_POST['user_name']))) {
                $bln = false;
                $strMsg = '请输入3~20位由小写字母或数字组成的用户名!';
            }
            if ($bln && isset($_POST['nick_name']) && !empty($_POST['nick_name'])) {
                if (!$this->mdlManager->isNickName($_POST['nick_name'])) {
                    $bln = false;
                    $strMsg = '昵称由3~20个字符组成!';
                }
            } elseif ($bln) {
                $_POST['nick_name'] = $_POST['user_name'];
            }
            if ($bln && isset($_POST['password']) && !empty($_POST['password'])) {
                if (!$this->mdlManager->isPassword($_POST['password'])) {
                    $bln = false;
                    $strMsg = '密码由6~20位字母或数字组成!';
                }
            } else {
                $_POST['password'] = 'abc123';
            }
            if ($bln && isset($_POST['email']) && !empty($_POST['email'])) {
                if (!Helper::isEmail($_POST['email'])) {
                    $bln = false;
                    $strMsg = 'Email格式错误!';
                }
            }
            if ($bln && isset($_POST['phone']) && !empty($_POST['phone'])) {
                if (!$this->mdlManager->isPhone($_POST['phone'])) {
                    $bln = false;
                    $strMsg = '请输入正确的固定电话或手机号码!';
                }
            }
            if ($bln) {
                $one = $this->mdlManager->getOne(array('user_name' => $_POST['user_name']));
                if ($one) {
                    $bln = false;
                    $strMsg = '用户名已存在!';
                }
            }
            if ($bln) {
                $_POST['password'] = sha1($_POST['password']);
                $one = $this->mdlManager->insert($_POST);
                if (!$one) {
                    $strMsg = '未能成功添加管理员,请检查!';
                } else {
                    $arrAssign['strOk'] = '添加成功, ID:' . $one . ' , <a href="' . HTTP_URL . '/index.php?m=admin&c=manager&a=edit&id=' . $one . '">编辑?</a>';
                    $_POST = array();
                }
            }
            $arrAssign['arrData'] = $_POST;
        }
        $arrAssign['strMsg'] = $strMsg;
        $this->_display('admin/manager_edit.php', $arrAssign);
    }
    
    public function edit() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'manager', 'update')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=manager', '你无权修改!');
        }
        $arrAssign = array(
            'blnAdd' => false,
            'strTitle' => '编辑管理员',
            'arrData' => array(),
            'arrGroup' => $this->arrGroup,
            'strMsg' => '',
            'strOk' => '',
            'id' => 0
        );
        $strMsg = '';
        $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
        $arrData = array();
        if ($id > 0) {
            $arrData = $this->mdlManager->getOne($id);
            $arrAssign['id'] = $id;
        }
        if (!$arrData) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=manager', '参数错误!');
        } else {
            $arrAssign['arrData'] = $arrData;
        }
        if (isset($_POST['sub_btn'])) {
            $arrUpdate = array();
            $bln = true;
            if (isset($_POST['group_name']) && isset($this->arrGroup[$_POST['group_name']])) {
                $arrUpdate['group_name'] = $_POST['group_name'];
            }
            if (isset($_POST['nick_name']) && !empty($_POST['nick_name'])) {
                if (!$this->mdlManager->isNickName($_POST['nick_name'])) {
                    $bln = false;
                    $strMsg = '昵称由3~20个字符组成!';
                } else {
                    $arrUpdate['nick_name'] = $_POST['nick_name'];
                }
            }
            if ($bln && isset($_POST['password']) && !empty($_POST['password'])) {
                if (!$this->mdlManager->isPassword($_POST['password'])) {
                    $bln = false;
                    $strMsg = '密码由6~20位字母或数字组成!';
                } else {
                    $arrUpdate['password'] = sha1($_POST['password']);
                }
            }
            if ($bln && isset($_POST['email']) && !empty($_POST['email'])) {
                if (!Helper::isEmail($_POST['email'])) {
                    $bln = false;
                    $strMsg = 'Email格式错误!';
                } else {
                    $arrUpdate['email'] = $_POST['email'];
                }
            }
            if ($bln && isset($_POST['phone']) && !empty($_POST['phone'])) {
                if (!$this->mdlManager->isPhone($_POST['phone'])) {
                    $bln = false;
                    $strMsg = '请输入正确的固定电话或手机号码!';
                } else {
                    $arrUpdate['phone'] = $_POST['phone'];
                }
            }
            if ($bln) {
                if (isset($_POST['rec_status']) && 1 == $_POST['rec_status']) {
                    $arrUpdate['rec_status'] = 1;
                } else {
                    $arrUpdate['rec_status'] = 0;
                    if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'manager', 'delete')) {
                        $bln = false;
                        $strMsg = '你无权设置为不可用!';
                    }
                }
            }
            if ($bln) {
                if (count($arrData) > 0 && $this->mdlManager->update($arrUpdate, $id)) {
                    $arrAssign['strOk'] = '更新成功!';
                    $arrUpdate['id'] = $id;
                    $arrUpdate['user_name'] = $arrData['user_name'];
                    $arrAssign['arrData'] = $arrUpdate;
                } else {
                    $strMsg = '保存失败,请检查!';
                }
            }
        }
        $arrAssign['strMsg'] = $strMsg;
        $this->_display('admin/manager_edit.php', $arrAssign);
    }
    
    public function clean() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'manager', 'delete')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=manager', '你无权删除!');
        }
        $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
        $strMsg = '参数错误!';
        if ($id > 0 && $this->mdlManager->getOne($id)) {
            $this->mdlManager->delete($id);
            $strMsg = '删除成功!';
        }
        Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=manager', $strMsg);
    }
    
}